package com.sangfor.ssl.service.setting;

import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.utils.IGeneral;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import kotlin.jvm.internal.LongCompanionObject;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes2.dex */
public class SettingManager {
    public static final String GLOBAL_AUTO_LOGIN_OFF = "global_auto_login_off";
    public static final String GLOBAL_HOST_DOMAIN_KEY = "global_host_domain";
    public static final String GLOBAL_HOST_KEY = "global_host";
    public static final String GLOBAL_LANGUAGE = "global_language";
    public static final String GLOBAL_SHUTDOWN_STATE = "shutdown_state";
    public static final String GLOBAL_TIMEQRY_UPDATE = "timeqry_update";
    public static final String GLOBAL_URL_KEY = "global_url";
    public static final String KEY_CONF_RESULT = "result.conf.key";
    public static final String KEY_L3VPN_HOST = "key.string.L3VPN_HOST";
    public static final String KEY_LOGIN_AUTH_RESULT = "result.loginAuth.key";
    public static final String KEY_RCLIST_RESULT = "result.rclist.key";
    public static final int MAX_SETTING_COUNT = 10;
    public static final String SETTING_USERNAME = "settings.userName";
    public static final int TIMEQRY_QUERY = 1;
    public static final int TIMEQRY_UPDATE = 0;
    public static final String TWFID_HOST = "twfid.host";
    public static final String TWFID_LOGOUT = "twfid.logout";
    public static final String TWFID_REALLINE = "twfid.realLine";
    public static final String TWFID_TWFID = "twfid.twfid";
    public static boolean isAbleWork = true;
    private static SettingManager msInstance;
    private JSONObject mSettingMap = new JSONObject();
    private JSONObject mTwfidMap = new JSONObject();
    private HashMap<String, Object> mGlobalMap = new HashMap<>();

    private SettingManager() {
    }

    public static synchronized SettingManager getInstance() {
        SettingManager settingManager;
        synchronized (SettingManager.class) {
            if (msInstance == null) {
                msInstance = new SettingManager();
            }
            settingManager = msInstance;
        }
        return settingManager;
    }

    public static synchronized void destoryObject() {
        synchronized (SettingManager.class) {
            msInstance = new SettingManager();
        }
    }

    private void removeFirstSetting() {
        boolean z;
        Iterator<String> keys = this.mSettingMap.keys();
        String str = null;
        long j = LongCompanionObject.MAX_VALUE;
        while (true) {
            if (!keys.hasNext()) {
                z = false;
                break;
            }
            String next = keys.next();
            JSONObject optJSONObject = this.mSettingMap.optJSONObject(next);
            if (optJSONObject == null) {
                this.mSettingMap.remove(next);
                z = true;
                break;
            }
            long optLong = optJSONObject.optLong("lastLoginTime", 0L);
            if (optLong < j) {
                str = next;
                j = optLong;
            }
        }
        if (str == null || z) {
            return;
        }
        this.mSettingMap.remove(str);
    }

    public String getString(String str, String str2) {
        Object obj = get(str, str2);
        return !(obj instanceof String) ? str2 : (String) obj;
    }

    public int getInt(String str, int i) {
        Object obj = get(str, Integer.valueOf(i));
        return !(obj instanceof Integer) ? i : ((Integer) obj).intValue();
    }

    public boolean getBoolean(String str, boolean z) {
        Object obj = get(str, Boolean.valueOf(z));
        return !(obj instanceof Boolean) ? z : ((Boolean) obj).booleanValue();
    }

    public Object get(String str, Object obj) {
        Object obj2 = get(str);
        return obj2 == null ? obj : obj2;
    }

    public Object get(String str) {
        Object obj;
        if (str == null || str.trim().equals("")) {
            return null;
        }
        String[] split = str.split("\\.");
        if (split.length < 2) {
            return null;
        }
        String str2 = split[0];
        int length = split.length;
        if (str2.equals("settings")) {
            String str3 = (String) getGlobalValue(GLOBAL_URL_KEY);
            if (str3 == null) {
                return null;
            }
            obj = this.mSettingMap.opt(str3);
        } else {
            obj = str2.equals("twfid") ? this.mTwfidMap : null;
        }
        if (obj == null) {
            return null;
        }
        for (int i = 1; i < length; i++) {
            obj = ((JSONObject) obj).opt(split[i]);
            if (obj == null) {
                return null;
            }
        }
        return obj;
    }

    public void set(String str, Object obj) {
        if (str == null || str.trim().equals("") || obj == null) {
            return;
        }
        String[] split = str.split("\\.");
        if (split.length < 2) {
            return;
        }
        String str2 = split[0];
        JSONObject jSONObject = null;
        synchronized (SettingManager.class) {
            try {
                if (str2.equals("settings")) {
                    String str3 = (String) getGlobalValue(GLOBAL_URL_KEY);
                    if (str3 == null) {
                        return;
                    }
                    jSONObject = (JSONObject) this.mSettingMap.opt(str3);
                    if (jSONObject == null) {
                        if (this.mSettingMap.length() >= 10) {
                            removeFirstSetting();
                        }
                        jSONObject = new JSONObject();
                        this.mSettingMap.put(str3, jSONObject);
                    }
                } else if (str2.equals("twfid")) {
                    jSONObject = this.mTwfidMap;
                }
                for (int i = 1; i < split.length - 1; i++) {
                    String str4 = split[i];
                    JSONObject optJSONObject = jSONObject.optJSONObject(str4);
                    if (optJSONObject == null) {
                        optJSONObject = new JSONObject();
                        jSONObject.put(str4, optJSONObject);
                    }
                    jSONObject = optJSONObject;
                }
                if (jSONObject != null) {
                    jSONObject.put(split[split.length - 1], obj);
                }
            } catch (JSONException unused) {
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.sangfor.ssl.service.setting.SettingManager$1Rec  reason: invalid class name */
    /* loaded from: classes2.dex */
    public class C1Rec {
        public long time;
        public String url;

        public C1Rec(String str, long j) {
            this.url = str;
            this.time = j;
        }

        public String toString() {
            return String.format("{url=\"%s\", time=%d}", this.url, Long.valueOf(this.time));
        }
    }

    public ArrayList<String> getHistoryUrls() {
        ArrayList<String> arrayList = new ArrayList<>();
        if (this.mSettingMap == null) {
            return arrayList;
        }
        ArrayList arrayList2 = new ArrayList(this.mSettingMap.length());
        Iterator<String> keys = this.mSettingMap.keys();
        int i = 0;
        while (keys.hasNext()) {
            String next = keys.next();
            long optLong = this.mSettingMap.optJSONObject(next).optLong("lastLoginTime", 0L);
            int i2 = 0;
            while (i2 < i && ((C1Rec) arrayList2.get(i2)).time < optLong) {
                i2++;
            }
            arrayList2.add(i2, new C1Rec(next, optLong));
            i++;
        }
        Collections.sort(arrayList2, new Comparator<C1Rec>() { // from class: com.sangfor.ssl.service.setting.SettingManager.1
            @Override // java.util.Comparator
            public int compare(C1Rec c1Rec, C1Rec c1Rec2) {
                return (int) (c1Rec2.time - c1Rec.time);
            }
        });
        for (int i3 = 0; i3 < i; i3++) {
            arrayList.add(((C1Rec) arrayList2.get(i3)).url);
        }
        return arrayList;
    }

    public void setGlobalValue(String str, Object obj) {
        if (this.mGlobalMap == null) {
            this.mGlobalMap = new HashMap<>();
        }
        this.mGlobalMap.put(str, obj);
    }

    public Object getGlobalValue(String str, Object obj) {
        Object obj2 = this.mGlobalMap.get(str);
        return obj2 == null ? obj : obj2;
    }

    public Object getGlobalValue(String str) {
        HashMap<String, Object> hashMap = this.mGlobalMap;
        if (hashMap == null) {
            Log.debug(IGeneral.LOG_TAG_SETTING, "globalMap has been cleanup when logged out");
            return null;
        }
        return hashMap.get(str);
    }

    public void removeSetting(String str) {
        synchronized (SettingManager.class) {
            this.mSettingMap.remove(str);
        }
    }

    public int getSettingCount() {
        return this.mSettingMap.length();
    }
}
